package com.fc.v2.service;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.RandomUtil;
import com.fc.v2.util.DateUtils;
import com.fc.v2.util.UuidGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.v2.common.base.BaseService;
import com.fc.v2.common.support.ConvertUtil;
import com.fc.v2.mapper.auto.SessionMapper;
import com.fc.v2.model.auto.Session;
import com.fc.v2.model.auto.SessionExample;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.util.SnowflakeIdWorker;
import com.fc.v2.util.StringUtils;

/**
 * wx登录session SessionService
 * @Title: SessionService.java 
 * @Package com.fc.v2.service 
 * @author fuce_自动生成
 * @email ${email}
 * @date 2021-03-04 16:31:25  
 **/
@Service
public class SessionService implements BaseService<Session, SessionExample>{
	@Autowired
	private SessionMapper sessionMapper;
	@Autowired
	private UuidGenerator uuidGenerator;

	/**
	 * 通过uid获取openid
	 * @param uid
	 * @return
	 */
	public String getOpenId(String uid){
		SessionExample e = new SessionExample();
		e.createCriteria().andUserIdEqualTo(uid);
		List<Session> sessions = selectByExample(e);
		if(sessions.size()==1)
			return sessions.get(0).getOpenid();
		else
			throw new RuntimeException("获取openID失败,该uid无对应openID");
	}
      	   	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<Session> list(Tablepar tablepar,Session session){
	        SessionExample testExample=new SessionExample();
			//搜索
			if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
	        	testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
	        }else {//大搜索
	        	testExample.createCriteria().andLikeQuery(session);
	        }
			//表格排序
			//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
	        //	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
	        //}else{
	        //	testExample.setOrderByClause("id ASC");
	        //}
	        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
	        List<Session> list= sessionMapper.selectByExample(testExample);
	        PageInfo<Session> pageInfo = new PageInfo<Session>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
					
			Integer[] integers = ConvertUtil.toIntArray(",", ids);
			List<Integer> stringB = Arrays.asList(integers);
			SessionExample example=new SessionExample();
			example.createCriteria().andIdIn(stringB);
			return sessionMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public Session selectByPrimaryKey(String id) {
				
			Integer id1 = Integer.valueOf(id);
			return sessionMapper.selectByPrimaryKey(id1);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(Session record) {
	 	record.setGainTime(DateUtils.getNowDate());
		return sessionMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(Session record) {
				
		record.setId(null);
		record.setGainTime(DateUtils.getNowDate());
				
		return sessionMapper.insertSelective(record);
	}


	/**
	 * 插入新数据或更新
	 * @param record
	 * @return
	 */
	public void insertOrUpdate(Session record) {
		String openid = record.getOpenid();
		SessionExample e = new SessionExample();
		e.createCriteria().andOpenidEqualTo(openid);

		List<Session> list = selectByExample(e);
		if(list.size() == 0){
			//插入
			record.setUserId(uuidGenerator.gen());
			insertSelective(record);
		}else{
			//更新skey，其他不变
			Session session = list.get(0);
			String newSkey = record.getSkey();

			record.setId(session.getId());
			record.setUserId(session.getUserId());
			record.setSkey(newSkey);
			updateByPrimaryKeySelective(record);
		}
	}


	
	
	@Override
	public int updateByExampleSelective(Session record, SessionExample example) {
		
		return sessionMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(Session record, SessionExample example) {
		
		return sessionMapper.updateByExample(record, example);
	}

	@Override
	public List<Session> selectByExample(SessionExample example) {
		
		return sessionMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(SessionExample example) {
		
		return sessionMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(SessionExample example) {
		
		return sessionMapper.deleteByExample(example);
	}


}
